Completed
Push — master ( e3ee1d...5cbb1b )
by Justin
01:36
created

module.exports   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 141

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 141
rs 8.2857

8 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 14 3
A 0 5 1
A 0 5 1
A 0 9 2
A 0 14 3
A 0 3 1
A 0 3 1
B 0 20 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
module.exports = function(GedcomX){
2
3
  var utils = require('../utils'),
4
      Base = require('../Base');
5
  
6
  /**
7
   * A list of Links
8
   * 
9
   * @constructor
10
   * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
11
   */
12
  var Links = function(json){
13
    
14
    // Protect against forgetting the new keyword when calling the constructor
15
    if(!(this instanceof Links)){
16
      return new Links(json);
17
    }
18
    
19
    // If the given object is already an instance then just return it. DON'T copy it.
20
    if(Links.isInstance(json)){
21
      return json;
22
    }
23
    
24
    this.init(json);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
25
  };
26
  
27
  // There's no value in us extending Base at the moment
28
  Links.prototype = Object.create(Base.prototype);
29
  
30
  Links._gedxClass = Links.prototype._gedxClass = 'GedcomX.Links';
31
  
32
  /**
33
   * Check whether the given object is an instance of this class.
34
   * 
35
   * @param {Object} obj
36
   * @returns {Boolean}
37
   */
38
  Links.isInstance = function(obj){
39
    return utils.isInstance(obj, this._gedxClass);
40
  };
41
42
  /**
43
   * Initialize from JSON
44
   * 
45
   * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
46
   * @return {Link} this
47
   */
48
  Links.prototype.init = function(json){
49
    
50
    Base.prototype.init.call(this, json);
51
    
52
    if(json){
53
      this.setLinks(json);
54
    }
55
    return this;
56
  };
57
  
58
  /**
59
   * Get the list of links
60
   * 
61
   * @return {Link[]} links
62
   */
63
  Links.prototype.getLinks = function(){
64
    return this.links;
65
  };
66
  
67
  /**
68
   * Get a link matching a rel
69
   * 
70
   * @param {String} rel
71
   * @return {Link} link
72
   */
73
  Links.prototype.getLink = function(rel){
74
    return this.links.find(function(link){
75
      return link.getRel() === rel;
76
    });
77
  };
78
  
79
  /**
80
   * Add a link
81
   * 
82
   * @param {Link} link
83
   * @return {Links} this
84
   */
85
  Links.prototype.addLink = function(link){
86
    // TODO: check for duplicates
87
    this.links.push(GedcomX.Link(link));
88
    return this;
89
  };
90
  
91
  /**
92
   * Set the links. May either provide the JSON object structure or an array
93
   * of Link instances
94
   * 
95
   * @param {Object|Link[]} links
96
   * @return {Links} this
97
   */
98
  Links.prototype.setLinks = function(links){
99
    this.links = [];
100
    if(links){
101
      
102
      // List of link
103
      if(Array.isArray(links)){
104
        for(var i = 0; i < links.length; i++){
105
          this.addLink(links[i]);
106
        }
107
      }
108
      
109
      // JSON object
110
      else {
111
        for(var rel in links){
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
112
          this.addLink(new GedcomX.Link(links[rel]).setRel(rel));
113
        }
114
      }
115
    }
116
    return this;
117
  };
118
  
119
  /**
120
   * Export the object as JSON
121
   * 
122
   * @return {Object} JSON object
123
   */
124
  Links.prototype.toJSON = function(){
125
    var links = this.getLinks(),
126
        json = {},
127
        linkJson, rel;
128
    for(var i = 0; i < links.length; i++){
129
      linkJson = utils.toJSON(links[i]);
130
      rel = linkJson.rel;
131
      if(rel){
132
        delete linkJson.rel;
133
        json[rel] = linkJson;
134
      }
135
    }
136
    return json;
137
  };
138
  
139
  GedcomX.Links = Links;
140
141
};